package cn.lf.android.lawfirm.undertake.apply.controler;


import android.content.Context;

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.undertake.apply.modle.UndertakeApply;
import cn.lf.android.lawfirm.undertake.apply.modle.UndertakeApplyInfo;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;

public class ApplyUnderInfoConroller extends BaseController {
    ApplyUnderInfoCallback mUnderInfoCallback;
    private Context context;

    public ApplyUnderInfoConroller(ApplyUnderInfoCallback callback, Context context) {
        this.mUnderInfoCallback = callback;
        this.context = context;
    }

    /**
     * 申请编号，承办审请列表中的sid字段
     *
     * @param sid
     */
    public void onApplyUnderInfo(String sid) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("applyID", sid);
        DebugLog.e("applyID==" + sid);
        new MyOkHttpClient().doGet(AppConfig.Apply_Undertake_Info2, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("承办申请信息beans==" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                responsObject = myJsonObject.getJSONObject("responsObject");
                                UndertakeApplyInfo beans = JSON.parseObject(responsObject.toString(), UndertakeApplyInfo.class);
                                DebugLog.e("beans==" + beans);
                                mUnderInfoCallback.onApplyUnderInfouccessed(beans);
                            } else {
                                mUnderInfoCallback
                                        .onApplyUnderInfoFailed("请求失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mUnderInfoCallback.onApplyUnderInfoFailed(errorMsg);
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }


    public interface ApplyUnderInfoCallback {
        public void onApplyUnderInfouccessed(UndertakeApplyInfo beans);

        public void onApplyUnderInfoFailed(String errorMsg);

    }
}
